package com.workflow.engine.eb.util;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/7.
 */
public class MapUtil {

    /**
     * 获取的queryString是没有问号的
     * @param parametersMap
     * @param isUrlEncoding
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getQueryStringFromMap(Map parametersMap, boolean isUrlEncoding) throws UnsupportedEncodingException {
        StringBuffer sbuffer = new StringBuffer();
        for(Object obj:parametersMap.keySet()){
            String value=(String) parametersMap.get(obj);
            if(!StringUtils.isBlank(value)){
                parametersMap.put(obj, value);
            }

            sbuffer.append(obj).append("=").append(value).append("&");
        }
        String value = sbuffer.toString().replaceAll("&$", "");
        if(isUrlEncoding){
            value = URLEncoder.encode(value, "utf8");
        }
        return value;
    }

}
